package org.spring.demo.service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.spring.demo.entity.Building;
import org.spring.demo.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

@Service
public class CityService {

	@Autowired
    @Qualifier("bdlpdbMongoTemplate")
    private MongoTemplate bdlpdbMongoTemplate;

	public List<City> getCityList() {
		List<Building> list = new ArrayList<Building>() ;
		DBCursor ds = bdlpdbMongoTemplate.getCollection("building").find();
		int count=0;
		long sTime = System.currentTimeMillis();
		System.out.println(sTime);
		for(DBObject d:ds){
			Building building = new Building();
			try {
				building.setCommunityName(d.get("communityName").toString());
			} catch (Exception e) {
			}
			count++;
			if(count%1000==0){
				System.out.println(count);
			}
		}
		long eTime = System.currentTimeMillis();
		System.out.println(eTime-sTime);
		System.out.println(".............................."+count);
		list = bdlpdbMongoTemplate.findAll(Building.class);
		long lTime = System.currentTimeMillis();
		System.out.println(lTime-eTime);
		
		BasicDBList endList = new BasicDBList();
        BasicDBObject autoStart = new BasicDBObject();
        BasicDBObject autoEnd = new BasicDBObject();
        BasicDBObject basicDBObject = new BasicDBObject();
        Pattern pattern = Pattern.compile("^.*加州.*$",Pattern.CASE_INSENSITIVE);
        autoEnd.append("communityName", pattern);
        autoStart.append("address", pattern);
        endList.add(autoStart);
        endList.add(autoEnd);
        basicDBObject.append("$or",endList);
        basicDBObject.append("districtName", "渝北区");
        DBCursor docs = bdlpdbMongoTemplate.getCollection("test").find(basicDBObject);
        for(DBObject doc:docs){
        	System.out.println(doc.get("name"));
        }
		return null;
	}
    
}
